const registerBox = document.querySelector('.register')
const loginBox = document.querySelector('.login')

const registerA = document.querySelector('.register-a')
const loginA = document.querySelector('.login-a')

//#region 登陆注册切换
registerA.addEventListener('click', function () {
  registerBox.style.display = 'none'
  loginBox.style.display = 'block'
})
loginA.addEventListener('click', function () {
  loginBox.style.display = 'none'
  registerBox.style.display = 'block'
})
//#endregion

//#region 登陆
const loginForm = document.querySelector('.login form')
loginForm.addEventListener('submit', function (e) {
  e.preventDefault()
  const data = $(loginForm).serialize()
  axios.post('/api/login', data).then(result => {

    if (result.data.code === 0) {
      toastr.success(result.data.message)
      const token = result.data.token
      localStorage.setItem('token', token)

      // location.href = "/index.html";// 根路径 绝对路径
      // location.href = "../../index.html";// 相对路径
      // location.href = "./index.html";// 相对路径

      // 建议 打开页面的方式 选择 open with live server
      // 设置页面的路径 选择 相对路径  location.href = "./index.html"
      setTimeout(function () {
        location.href = './index.html'
      }, 1500)
    } else {
      toastr.error(result.data.message)
    }

  })
})
//#endregion

//#region 注册
const registerForm = document.querySelector('.register form')
registerForm.addEventListener('submit', function (e) {
  e.preventDefault()
  const data = $(registerForm).serialize()
  axios.post('/api/register', data).then(result => {
    if (result.data.code === 0) {
      toastr.success(result.data.message)
    } else {
      toastr.warning(result.data.message)
    }
  })
})

//#endregion

//#region 正则检测
// const uname = document.querySelectorAll('input[name=username]')
// uname.forEach(item => {
//       item.addEventListener('change', function () {
//           if (!item.value) {

//           }
//         }
//       })

//     const upws = document.querySelectorAll('input[name=password]')
//#endregion